<template>
    <div class="register-container">
        <div class="background">
            <el-card class="box-card">
                <h2 style="text-align: center; font-weight: 700;">用户注册</h2>
                <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                    <el-form-item label="用户名" prop="username">
                        <el-input v-model="ruleForm.username"></el-input>
                    </el-form-item>
                    <el-form-item label="密码" prop="password">
                        <el-input v-model="ruleForm.password" type="password"></el-input>
                    </el-form-item>
                    <el-form-item label="昵称" prop="nickname">
                        <el-input v-model="ruleForm.nickname"></el-input>
                    </el-form-item>
                    <el-form-item label="手机号" prop="phone">
                        <el-input v-model="ruleForm.phone"></el-input>
                    </el-form-item>
                    <el-form-item label="用户类型" prop="type">
                        <el-radio-group v-model="ruleForm.type">
                            <el-radio :label="2">房东</el-radio>
                            <el-radio :label="1">租客</el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item label="性别" prop="sex">
                        <el-select v-model="ruleForm.sex" placeholder="请选择性别">
                            <el-option label="男" value="1"></el-option>
                            <el-option label="女" value="0"></el-option>
                        </el-select>
                    </el-form-item>
                </el-form>
                <div style="text-align: center">
                    <el-button @click="submitForm('ruleForm')" style="background-color: #00C2B3; color: white; border: 0px;">注册</el-button>
                    <el-button @click="resetForm('ruleForm')" style="background-color: #00C2B3; color: white; border: 0px;">重置</el-button>
                </div>
            </el-card>
        </div>
    </div>

</template>

<script>
    export default {
        data() {
            return {
                ruleForm: {
                    username: '',
                    password: '',
                    nickname: '',
                    phone: '',
                    type: 2,
                    sex: ''
                },
                rules: {
                    username: [
                        { required: true, message: '请输入用户名', trigger: 'blur' },
                        { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
                    ],
                    password: [
                        { required: true, message: '请输入密码', trigger: 'change' },
                        { min: 6, max: 12, message: '密码长度在6到12个字符', trigger: 'blur' }
                    ],
                    type: [
                        { required: true, message: '请选择用户类型', trigger: 'change' }
                    ],
                    phone: [
                        { required: true, message: '请输入手机号', trigger: 'change' }
                    ],
                    sex: [
                        { required: true, message: '请选择用户性别', trigger: 'change' }
                    ]
                }
            };
        },
        methods: {
            submitForm(formName) {
                const _this = this
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        _this.$http.post('/user/register', _this.ruleForm).then(function (resp) {
                            if (resp.data.code === 205)
                            {
                                _this.$message.success('注册成功');
                                _this.$router.push('/')
                            } else {
                                _this.$message.error(resp.data.data)
                            }
                        })
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        }
    }
</script>

<style scoped>
    .background{
        position: absolute;
        background: url("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic1.win4000.com%2Fwallpaper%2F2018-03-16%2F5aab63425b2c7.jpg&refer=http%3A%2F%2Fpic1.win4000.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1617455019&t=dbd993f531ed74149deda7c3dc9ab4ec") no-repeat center;
        width: 100%;
        height: 100%;
    }

    .text {
        font-size: 14px;
    }

    .item {
        padding: 18px 0;
    }

    .box-card {
        width: 480px;
        margin: 100px auto;
    }
    >>> .el-radio__input.is-checked .el-radio__inner {
        border-color: #00C2B3;
        background: #00C2B3;
    }

    >>> .el-radio__input.is-checked+.el-radio__label {
        color: #00C2B3;
    }

    .el-select-dropdown__item.selected {
        color: #00C2B3;
        font-weight: 700;
    }
</style>
